import 'package:json_annotation/json_annotation.dart';
import 'package:juming_test/model/domain/domain_filter_option_model.dart';

part 'domain_filter_item_model.g.dart';

@JsonSerializable(explicitToJson: true, includeIfNull: false)
class DomainFilterItemModel {
  DomainFilterItemModel(
    this.pid,
    this.name,
    this.key,
    this.value,
    this.inputValue1,
    this.inputValue2,
    this.type,
    this.hint,
    this.options,
    this.groupItems,
    this.derivativeItems,
    this.derivativeValue,
    this.pickData,
  );

  @JsonKey(defaultValue: 0)
  int pid;
  @JsonKey(defaultValue: '')
  String name;
  @JsonKey(defaultValue: '')
  String key;
  // 主要用于存储单输入框的值
  @JsonKey(defaultValue: '')
  String value;
  // 双输入框对应的两个值
  @JsonKey(defaultValue: '')
  String inputValue1;
  @JsonKey(defaultValue: '')
  String inputValue2;
  // 条目类型
  @DomainFilterItemTypeConverter()
  DomainFilterItemType type;
  // 输入框默认显示
  @JsonKey(defaultValue: '')
  String hint;
  // 选项
  @JsonKey(defaultValue: [])
  List<DomainFilterOptionModel> options;
  // 组合选项（域名构成，特别构成）
  @JsonKey(defaultValue: [])
  List<DomainFilterItemModel> groupItems;
  // 衍生条目
  @JsonKey(defaultValue: [])
  List<DomainFilterItemModel> derivativeItems;
  // 衍生条目触发value
  @JsonKey(defaultValue: '')
  String derivativeValue;
  // picker数据
  @JsonKey(defaultValue: {})
  Map<String, dynamic> pickData;

  // 双输入的key
  String get inputKey1 => '${key}_1';
  String get inputKey2 => '${key}_2';

  factory DomainFilterItemModel.fromJson(Map<String, dynamic> json) =>
      _$DomainFilterItemModelFromJson(json);
  Map<String, dynamic> toJson() => _$DomainFilterItemModelToJson(this);
}

enum DomainFilterItemType {
  // 单选
  singleChoice,
  // 多选
  multipleChoice,
  // 单输入框（带复选框）
  singleInput,
  // 双输入框
  doubleInput,
  // 单选带双输入框
  singleChoiceWithInput,
  // 双picker
  doublePicker,
  // 单选分组展示
  groupSingleChoice,
}

// 消息类型转枚举
class DomainFilterItemTypeConverter
    implements JsonConverter<DomainFilterItemType, String> {
  const DomainFilterItemTypeConverter();

  @override
  DomainFilterItemType fromJson(String json) {
    switch (json) {
      case 's_choice':
        return DomainFilterItemType.singleChoice;
      case 'm_choice':
        return DomainFilterItemType.multipleChoice;
      case 'input':
        return DomainFilterItemType.singleInput;
      case 'input2':
        return DomainFilterItemType.doubleInput;
      case 'choice_input':
        return DomainFilterItemType.singleChoiceWithInput;
      case 'select2':
        return DomainFilterItemType.doublePicker;
      case 'choice_group':
        return DomainFilterItemType.groupSingleChoice;
      default:
        return DomainFilterItemType.singleChoice;
    }
  }

  @override
  String toJson(DomainFilterItemType object) {
    switch (object) {
      case DomainFilterItemType.singleChoice:
        return 's_choice';
      case DomainFilterItemType.multipleChoice:
        return 'm_choice';
      case DomainFilterItemType.singleInput:
        return 'input';
      case DomainFilterItemType.doubleInput:
        return 'input2';
      case DomainFilterItemType.singleChoiceWithInput:
        return 'choice_input';
      case DomainFilterItemType.doublePicker:
        return 'select2';
      case DomainFilterItemType.groupSingleChoice:
        return 'choice_group';
    }
  }
}
